package com.lc.YYGH.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;

import com.lc.YYGH.hosp.repository.DepartmentRepository;
import com.lc.YYGH.hosp.service.DepartmentService;
import com.lc.YYGH.model.hosp.Department;
import com.lc.YYGH.vo.hosp.DepartmentQueryVo;
import com.lc.YYGH.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentRepository departmentRepository;
    @Override
    public void save(Map<String, Object> paramMap) {
        Department dep= JSONObject.parseObject(JSONObject.toJSONString(paramMap),Department.class);
        //从数据库中查到了科室
       Department department=departmentRepository.findByHoscodeAndDepcode(dep.getHoscode(),dep.getDepcode());
        if(null!=department)
        {
            //科室已经存在 更新
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);

        }
        else
        {
            Date now=new Date();
            dep.setCreateTime(now);
            dep.setUpdateTime(now);
            dep.setIsDeleted(0);
            departmentRepository.save(dep);
        }
    }

//    @Override
//    public Page<Department> findPageDepartment(int page, int limit, DepartmentQueryVo departmentQueryVo) {
//        Pageable pageable = PageRequest.of(page-1,limit);
//        // 创建Example对象
//        Department department = new Department();
//        //将Vo对象转为department
//        BeanUtils.copyProperties(departmentQueryVo,department);
//        department.setIsDeleted(0);
//        //构造条件查询规则
//        ExampleMatcher matcher = ExampleMatcher.matching()
//                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
//                .withIgnoreCase(true);
//
//        Example<Department> example = Example.of(department,matcher);
//
//        //查询所有对象
//        Page<Department> all = departmentRepository.findAll(example, pageable);
//        return all;
//    }

    @Override
    public Page<Department> findPageDepartment(int page, int limit, DepartmentQueryVo departmentQueryVo) {
        // 创建Pageable对象，设置当前页和每页记录数
        //0是第一页
        Pageable pageable = PageRequest.of(page-1,limit);
        // 创建Example对象
        Department department = new Department();
        //将Vo对象转为department
        BeanUtils.copyProperties(departmentQueryVo,department);
        department.setIsDeleted(0);
        //构造条件查询规则
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);

        Example<Department> example = Example.of(department,matcher);

        //查询所有对象
        Page<Department> all = departmentRepository.findAll(example, pageable);
        System.out.println(all.toString());
        return all;
    }

    @Override
    public void removeDepartment(String hoscode, String depcode) {
      Department department=departmentRepository.findByHoscodeAndDepcode(hoscode,depcode);
      if(null!=department)
      {
          departmentRepository.deleteById(department.getId());
      }
    }

    @Override
    public List<DepartmentVo> findDepTree(String hoscode) {
        List<DepartmentVo> list=new ArrayList<>();
        Department department=new Department();
        department.setHoscode(hoscode);
        Example example=Example.of(department);
        //从数据库中查询出
        List<Department> departmentList=departmentRepository.findAll(example);
        //按照大科室号分组
        Map<String,List<Department>> deparmentMap=departmentList.stream().collect(Collectors.groupingBy(e->e.getBigcode()));
        for(Map.Entry<String,List<Department>> entry:deparmentMap.entrySet())
        {
            //获取大科室编号
            String bigcode=entry.getKey();
          List<Department> value=  entry.getValue();

         String bigname= value.get(0).getBigname();
         DepartmentVo departmentVo=new DepartmentVo();
        departmentVo.setDepcode(bigcode);
        departmentVo.setDepname(bigname);


         // 创建小科室
            List<DepartmentVo> children=new ArrayList<>();
            for(Department dep:value){
                DepartmentVo dvo=new DepartmentVo();
                dvo.setDepcode(dep.getDepcode());
                dvo.setDepname(dep.getDepname());
                children.add(dvo);

            }
            //把小科室集合设置到大科室集合
            departmentVo.setChildren(children);
            list.add(departmentVo);
        }

        return list;
    }

    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department=departmentRepository.findByHoscodeAndDepcode(hoscode,depcode);
        return department.getDepname();
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        Department department=departmentRepository.findByHoscodeAndDepcode(hoscode,depcode);
        return department;
    }
}
